/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package diagram;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Roderick
 */
public class ZoomPanel extends JPanel {
        protected double zoom;

        public ZoomPanel(double initialZoom) {
            /*super(new FlowLayout());
            setName("Zoom Panel");*/

            super( );
            //setName("Zoom Panel");

            zoom = initialZoom;
        }

        public void paint(Graphics g) {
            super.paintComponent(g); // clears background
            Graphics2D g2 = (Graphics2D) g;
            AffineTransform backup = g2.getTransform();
            g2.scale(zoom, zoom);
            super.paint(g);
            g2.setTransform(backup);
        }

        public boolean isOptimizedDrawingEnabled() {
            return false;
        }

        public Dimension getPreferredSize() {
            Dimension unzoomed
              = getLayout().preferredLayoutSize(this);
            Dimension zoomed
              = new Dimension((int) ((double) unzoomed.width*zoom),
                              (int) ((double) unzoomed.height*zoom));
            System.out.println("PreferredSize: Unzoomed "+unzoomed);
            System.out.println("PreferredSize: Zoomed "+zoomed);
            return zoomed;
        }

        public void setZoom(double newZoom)  throws PropertyVetoException {
            if (newZoom <= 0.0) {
                throw new PropertyVetoException
                    ("Zoom must be positive-valued",
                     new PropertyChangeEvent(this,
                                             "zoom",
                                             new Double(zoom),
                                             new Double(newZoom)));
            }
            double oldZoom = zoom;
            if (newZoom != oldZoom) {
                Dimension oldSize = getPreferredSize();
                zoom = newZoom;
                Dimension newSize = getPreferredSize();
                firePropertyChange("zoom", oldZoom, newZoom);
                firePropertyChange("preferredSize",
                                   oldSize, newSize);
                revalidate();
                repaint();
            }
        }

        public double getZoom() {
            return zoom;
        }
    }





